package com.chen.pageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈俊宏
 */
public class ItemProcessor implements PageProcessor {
    private final Site site = Site.me().setRetryTimes(3).setSleepTime(100);
    static int allnumber = 0;
    @Override
    public void process(Page page) {
        List<String> all = page.getHtml().xpath("//li[@class=\"gl-item\"]").all();
        List<Map<String, Object>> list = new ArrayList<>();
        for (String s : all) {
            Html temp = Html.create(s);
            if (!temp.xpath("li[@class=gl-item]/@ware-type").get().equals("0")) {
                Map<String, Object> map = new HashMap<>();
                map.put("sku", temp.xpath("//li/@data-sku").toString());
                map.put("spu", temp.xpath("//li/@data-spu").toString());
                map.put("title", temp.xpath("//div[@class=\"p-name\"]/a/em/allText()").get());
                map.put("url", temp.xpath("//div[@class=\"p-img\"]/a/@href").get());
                map.put("img", temp.xpath("//div[@class=\"p-img\"]/a/img/@data-lazy-img").get());
                map.put("ad", temp.xpath("//div[@class=\"p-img\"]/a/@title").get().replaceAll("\n", ""));
                map.put("price", temp.xpath("//div[@class=\"p-price\"]/strong/i/text()").toString());
                map.put("characteristic", temp.xpath("//div[@class=\"p-name\"]/span/span[@class=\"attr\"]/b/text()").all());
                map.put("shopName", temp.xpath("//div[@class=\"p-shop\" or @class=\"p-shopnum\"]/allText()").get());
                map.put("shopUrl", temp.xpath("//a[@class=\"hd-shopname\"]/@href").get());
                map.put("type", page.getRequest().getExtra("type").toString());
                map.put("brand", page.getRequest().getExtra("brand").toString());
                list.add(map);
            }
        }
        page.putField("data", list);
        page.putField("type", "item");
        System.out.println("已经爬取的页面：" + (++allnumber) + " " + "要求登录的次数" + DemoProcessor.fail);
    }

    @Override
    public Site getSite() {
        return this.site;
    }
}
